package kr.co.happenstance;

import kr.co.happenstance.lib.MemberLib;
import kr.co.happenstance.lib.vo.LibResultVo;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class ClauseActivity extends BaseActivity implements View.OnClickListener{
	protected final String TAG="<<<ClauseActivity>>>";
	
	/**
	 * 약관 TextView 객체
	 */
	private TextView tvClause;
	
	/**
	 * 서비스 이용약관 동의 CheckBox 객체
	 */
	private CheckBox chkService;
	
	/**
	 * 개인정보 취급 방침 동의 CheckBox 객체
	 */
	private CheckBox chkPrivate;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_clause);
		Log.d(TAG, "onCreate......");
		
		tvClause = (TextView) this.findViewById(R.id.tv_clause);
		tvClause.setMovementMethod(new ScrollingMovementMethod());
		
		chkService = (CheckBox) this.findViewById(R.id.chk_service);
		chkPrivate = (CheckBox) this.findViewById(R.id.chk_private);
		
		addActivity(this);
	}
	
	@Override
	public void onClick(View v){
		switch(v.getId()){
			case R.id.btn_pop_close:
				Log.d(TAG, "btn_pop_close onClick.....");
				
				new CustomAlertDialogBuilder(this)
				.setTitle(R.string.alert_title)
				.setMessage(R.string.alert_msg_clause_pop_close)
				.setCancelable(true)
				.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
						appFinish();
					}
				})
				.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();	
					}
				})
				.create();					
				
				break;
				
			case R.id.btn_ok:
				Log.d(TAG, "btn_ok onClick.....");
				if(!chkService.isChecked() || !chkPrivate.isChecked()){
					int message_id = (!chkService.isChecked())?R.string.alert_msg_chk_service_error: R.string.alert_msg_chk_private_error;
					new CustomAlertDialogBuilder(ClauseActivity.this, R.string.alert_title, message_id).create();						
				}else{
					new JoinTask().execute();
				}
				
				break;
		}
	}
	
	
	private class JoinTask extends AsyncTask<Void, Void, LibResultVo> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showProgress();
		}		
		
		@Override
		protected LibResultVo doInBackground(Void... arg0) {
			LibResultVo resultVo = null;
			try{
				MemberLib lib =  new MemberLib();
				resultVo = lib.join(myApp.id, myApp.gender, myApp.birthday, myApp.email);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			return resultVo;
		}
		
		@Override
		protected void onPostExecute(LibResultVo resultVo) {
			closeProgress();
			
			if(resultVo == null || resultVo.getResult().equals(C.API_RESULT_VALUE_FAIL)){
				new CustomAlertDialogBuilder(ClauseActivity.this, R.string.alert_title, R.string.alert_msg_exception).create();			
			}else{
				Toast.makeText(ClauseActivity.this, R.string.toast_msg_join_success, Toast.LENGTH_SHORT).show();
				
				//회원가입후 다시 intro 시작
				Intent intent = new Intent(ClauseActivity.this, IntroActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				ClauseActivity.this.finish();
			}
			
		}
	}
	
}
